<template>
  <div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="图片" prop="pic">
        <el-upload
          class="avatar-uploader"
          action="http://upload-z1.qiniup.com"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="uploadData"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="选择标签" prop="tags">
        <el-checkbox-group v-model="ruleForm.tags">
          <el-checkbox
            :label="i.id"
            name="type"
            v-for="(i, index) in tags"
            :key="index"
          >{{i.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="选择分类" prop="ctype">
        <el-checkbox-group v-model="ruleForm.ctype">
          <el-checkbox
            :label="i.id"
            name="type"
            v-for="(i, index) in clist"
            :key="index"
          >{{i.name}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item label="选择类别" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio
            :label="i.id"
            name="type"
            v-for="(i, index) in types"
            :key="index"
          >{{i.name}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="简介" prop="desc">
        <el-input type="textarea" v-model="ruleForm.desc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm"
          >立即创建</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      uploadData:{"token":''},
      tags: [],
      types: [],
      clist: [],
      imageUrl: "",
      ruleForm: {
        name: "",
        tags: [],
        ctype: [],
        type: "",
        desc: "",
        url: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        ctype: [
          {
            type: "tags",
            required: true,
            message: "请至少选择一个分类",
            trigger: "change",
          },
        ],
        type: [
          { required: true, message: "请选择活动资源", trigger: "change" },
        ],
        tags: [{ required: true, message: "请填写活动形式", trigger: "blur" }],
      },
    };
  },
  methods: {
    //获取token
    getQntoken(){
      this.axios.get("user/get_qntoken").then((res) => {
        this.uploadData.token = res.data.token
      })
    },
    getTags() {
      this.axios.get("user/getTags").then((res) => {
        this.tags = res.data.tags;
        this.types = res.data.types;
        this.clist = res.data.clist;
      });
    },
    submitForm(formName) {
        //将图片地址写入
        this.ruleForm.url = this.imageUrl
        this.axios.post('user/addCourse', this.ruleForm).then((res) => {
          console.log("123");
          
        })

    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleAvatarSuccess(res) {
      alert(res.key)
      this.imageUrl = "http://rrlc5uvk3.hb-bkt.clouddn.com/"+res.key
    },
    beforeAvatarUpload(file) {
      

      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  },
  mounted() {
    this.getTags();
    this.getQntoken();
    }
}
</script>

<style>
</style>